// components/multiSelectMenu/multiSelectMenu.js
Component({
  properties: {
    show:{
      type:Boolean,
      value:false,
      observer(newValue) {
        // if(newValue) {
        //   this.submit()
        // }
      }
    },
    labels:['中国','美国','法国'],  //显示值
    values:[],  //对应实际值
    select:[]  //选中的值
  },
  data: {
    // labels:['中国','美国','法国'],  //显示值
    // values:[1,2,3],  //对应实际值
    // select:[1,2]  //选中的值
  },
  lifetimes: {
    attached() {
      this.submit()
      console.log('attached',this.data.select)
    }
  },
  methods: {
    closeMenu() {
      this.setData({
        show:false
      })
    },
    changeSelect(e) {
      let index = e.currentTarget.dataset.index
      let values = this.data.values
      let newSelect = [...this.data.select];
      if(newSelect.includes(values[index])) {
        newSelect.splice(newSelect.indexOf(values[index]), 1);
      }else {
        newSelect.push(values[index])
      }
      console.log(newSelect)
      this.setData({
        select:newSelect
      })
    },
    submit() {
      let label = []
      for(let i = 0;i<this.data.select.length;i++) {
        label.push(this.data.labels[this.data.values.indexOf(this.data.select[i])])
      }
      let data = {
        select:this.data.select,
        label:label.join(',')
      }
      this.triggerEvent('dataChange',data)
      this.closeMenu()
    }
  }
})